<?php
use yii\helpers\Url;
use app\common\widgets\Page;
function getStatusName($int_status) {
	switch($int_status) {
		case '1':
			$status_name = '已审核';
			break;
		case '2':
			$status_name = '已驳回';
			break;
		case '3':
			$status_name = '上线完成';
			break;
		case '4':
			$status_name = '上线失败';
			break;
		default:
			$status_name = '待审核';
			break;
	}
	return $status_name;
}
function getStatusClass($int_status) {
	switch($int_status) {
		case '1':
			$status_name = '';
			break;
		case '2':
			$status_name = 'text-warning';
			break;
		case '3':
			$status_name = 'text-success';
			break;
		case '4':
			$status_name = 'text-danger';
			break;
		default:
			$status_name = 'text-primary';
			break;
	}
	return $status_name;
}
?>
<section class="vbox">
	<section class="scrollable padder">
		<div class="m-b-md">

		</div>
		<section class="panel panel-default">
			<header class="panel-heading">
				项目列表
			</header>
			<div class="row wrapper">
				<div class="col-sm-5 m-b-xs">
					<a href="<?php echo \Yii::$app->urlManager->createUrl("available/add"); ?>" class="btn btn-sm btn-default"><i class="fa fa-plus"></i>添加</a>
				</div>
				<div class="col-sm-4 m-b-xs">

				</div>
				<form action="<?php echo \Yii::$app->urlManager->createUrl("available/lists"); ?>" method="post" class="form-horizontal">
					<div class="col-sm-3">
						<div class="input-group">
							<input type="text" class="input-sm form-control" placeholder="请输入搜索关键词" name="keyword" value="<?php echo $filter['keyword'];?>">
                          <span class="input-group-btn">
                            <button class="btn btn-sm btn-default" type="submit">搜索</button>
                          </span>
						</div>
					</div>
				</form>
			</div>
			<div class="table-responsive">
				<table class="table table-striped b-t b-light">
					<thead>
					<tr>
						<th style="width:20px;"><label class="checkbox m-n i-checks"><input type="checkbox"><i></i></label></th>
						<th>项目名称</th>
						<th>开发者</th>
						<th>上线单标题</th>
						<th>上线时间</th>
						<th>分支</th>
						<th>上线commit号</th>
						<th>当前状态</th>
						<th>操作</th>
					</tr>
					</thead>
					<tbody>
					<?php foreach((array)$data as $key=>$info):?>
						<tr id="list_<?php echo $info['id'];?>">
							<td><label class="checkbox m-n i-checks"><input type="checkbox" name="post[]"><i></i></label></td>
							<td><?php $env_string = "env_".$project_list[$info['project_id']]['level'];echo $project_list[$info['project_id']]['name'] ."-" .Yii::$app->params['logic_config'][$env_string];?></td>
							<td><?php echo $user_list[$info['user_id']]['username'];?></td>
							<td><?php echo $info['title'];?></td>
							<td><?php echo $info['created_time'];?></td>
							<td><?php echo $info['branch'];?></td>
							<td><?php echo $info['commit_id'];?></td>
							<td class="<?php echo getStatusClass($info['status']);?>"><?php echo getStatusName($info['status']); ?></td>
							<td>
								<label class="switch">
									<input type="checkbox" class="task-operation" checked data-id="<?php echo $info['id'];?>">
									<span></span>
								</label>
								<a href="javascript:;" class="btn btn-sm btn-default del_checked" title="删除"  data-original-title="删除" data-id="<?php echo $info['id'];?>" data-title="<?php echo $info['title'];?>" style="position: relative; top: -13px;margin-left: 5px;"> <i class="fa fa-trash-o text-muted"></i> <span class="text">删除</span></a>

							</td>
						</tr>
					<?php endforeach?>
					</tbody>
				</table>
			</div>
			<footer class="panel-footer">
				<div class="row">
					<div class="col-sm-4 hidden-xs">
						<select class="input-sm form-control input-s-sm inline v-middle">
							<option value="0">Bulk action</option>
							<option value="1">Delete selected</option>
							<option value="2">Bulk edit</option>
							<option value="3">Export</option>
						</select>
						<button class="btn btn-sm btn-default">应用</button>
					</div>
				</div>
			</footer>
		</section>
	</section>
	<footer class="footer bg-white b-t" data-type="ajaxPage">
		<div class="row text-center-xs">
			<div class="col-md-6 hidden-sm">
				<p class="text-muted m-t">总共<?php echo $total;?>条 当前为第<?php echo $filter['page'];?>页</p>
			</div>
			<div class="col-md-6 col-sm-12 text-right text-center-xs">
				<!--                				--><?//= LinkPager::widget(['pagination' => $pages]); ?>
				<?php if($total > 0):echo Page::model(array('count'=>$total,'size'=>$limit))->show("NewPage");endif;?>
			</div>
		</div>
	</footer>
</section>
<script>
	$(document).ready(function() {
		$('.task-operation').click(function() {
			$this = $(this);
			$.get("<?php echo \Yii::$app->urlManager->createUrl("available/operation"); ?>", {
						id: $this.data('id'),
						operation: $this.is(':checked') ? 1 : 0
					},
					function(msgObj) {
						if(msgObj.status == 0) {
							window.location.reload();
						} else {
							G.ui.tips.err(msgObj.message);
							return false;
						}
					}
			);
		})
		var $del_checked = $("a.del_checked");
		$del_checked.click(function () {
			var id = $(this).data("id");
			var name = $(this).data('title');
			bootbox.dialog({
				message: '<p>确定删除 <font style="color:red">'+name+'</font> ?',
				title: "确定删除此记录?",
				buttons: {
					danger: {
						label: "确定",
						className: "btn btn-success",
						callback: function() {
							$.ajax({
								url:"<?php echo \Yii::$app->urlManager->createUrl("available/delete"); ?>",
								cache:false,
								type:'POST',
								dataType:'json',
								data:{"id":id},
								success:function(msgObj){
									if(msgObj.status == '0'){
										$("#list_"+id).remove();
										G.ui.tips.suc(msgObj.message, "<?php echo \Yii::$app->urlManager->createUrl("available/lists"); ?>");
										return false;
									}else{
										G.ui.tips.err(msgObj.message);
										return false;
									}
								}
							});
						}
					},
					main: {
						label: "取消",
						className: "btn btn-default",
						callback: function() {}
					}
				}
			});
		});
	});


</script>